package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.dao.model.User;
import com.item.service.MessageService;
import com.item.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.Policy;
import com.zch.dao.model.PolicyExample;
import com.zch.dao.model.Xqmessage;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.PolicyEx;
import com.zch.daoEx.model.XqmessageEx;
import com.zch.service.PolicyService;

@RequestMapping("policy")
@Controller
@LoginFilter
public class PolicyController extends CoreController{

	@Autowired
	private PolicyService policyService;
	@Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<PolicyEx> list = policyService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Policy policy){
		if (StringUtils.isBlank(policy.getId())){
			policyService.insert(policy);
		}else{
			policyService.updateByPrimaryKeySelective(policy);
			policy = policyService.selectByPrimaryKey(policy.getId());
            String pushContent = "";
            if (policy.getIsAgree() == 2){
                pushContent = "审核未通过！原因:"+policy.getNotAgreeReason();
                messageService.findMessage(policy.getTitle(), pushContent, policy.getUserId(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Policy policy = policyService.selectByPrimaryKey(id);
		policy.setContent(policy.getContent().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(policy);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Policy policy = policyService.selectByPrimaryKey(id);
        policy.setContent(policy.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(policy);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			policyService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/update")
    @ResponseBody
    public String update(Policy policy){
        int result = policyService.updateByPrimaryKeySelective(policy);
        if(result == 1){
            policy = policyService.selectByPrimaryKey(policy.getId());
            String pushContent = "";
            if(policy.getIsAgree() == 1){
                 pushContent = "审核通过！";
                 messageService.findMessage(policy.getTitle(), pushContent, policy.getUserId(), 1);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    }
	
	@RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        List<PolicyEx> list = policyService.selectMyList(map);

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
	
	@RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Policy policy){
	    policy.setUserId(getSessionParameter("id"));
	    if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
	    User user = userService.selectByPrimaryKey(getSessionParameter("id"));
        if (StringUtils.isBlank(policy.getId())){
            policyService.insert(policy);
        }else{
            policyService.updateByPrimaryKeySelective(policy);
        }
        return JSONUtils.serialize(new Ret(0));
    }
	
	@RequestMapping("/top")
    @ResponseBody
    public String top(Policy policy){
        if (StringUtils.isBlank(policy.getId())){
            policy.setPublishTime(new Date());
            policyService.insert(policy);
        }else{
            policy.setPublishTime(new Date());
            policyService.updateByPrimaryKeySelective(policy);
        }
        return JSONUtils.serialize(new Ret(0));
    }
	
	@RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(Policy policy){
        if (StringUtils.isBlank(policy.getId())){
            policy.setPublishTime(null);
            policyService.insert(policy);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", policy.getId());
            int result = policyService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}